/**
 * Copyright (c) 时间, Tal.Ben 袁东昊 (ydonghao2@gmail.com).
 *
 * 版权
 */
package com.talben.soudian.fragment;

import java.io.Serializable;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.talben.soudian.AppContext;
import com.talben.soudian.adapter.ListBaseAdapter;
import com.talben.soudian.adapter.NewApkAdapter;
import com.talben.soudian.api.SouDianAPI;
import com.talben.soudian.base.BaseListFragment;
import com.talben.soudian.bean.ApkList;
import com.talben.soudian.bean.Gift;
import com.talben.soudian.bean.GiftList;
import com.talben.soudian.bean.SimpleBackPage;
import com.talben.soudian.ui.EmptyLayout;
import com.talben.soudian.ui.OnTabReselectListener;
import com.talben.soudian.ui.UIHelper;

/**
 * @Title: NewApkFragment.java
 * @Package com.talben.soudian.fragment
 * @Description: 新的Apk碎片
 * @author Tal.Ben
 * @date 2015年6月14日 上午9:54:09
 * @version v1.0
 */
public class NewApkFragment extends BaseListFragment<Gift> implements
		OnTabReselectListener {

	private static final String CACHE_KEY_PREFIX = "newapkslist_";

	@Override
	public void onTabReselect() {
		onRefresh();
	}

	@Override
	protected NewApkAdapter getListAdapter() {
		return new NewApkAdapter();
	}

	@Override
	protected String getCacheKeyPrefix() {
		return CACHE_KEY_PREFIX + mCatalog;
	}

	@Override
	protected GiftList parseList(JSONArray JArray) throws Exception {
		GiftList list = null;
        try {
            list = new GiftList();
            for (int i = 0; i < JArray.length(); i++) {
            	JSONObject jObj = JArray.getJSONObject(i);
            	Gift gift = new Gift();
            	gift.setGicon(jObj.getString("icon"));
            	gift.setGiftname(jObj.getString("zone"));
            	gift.setDescription(jObj.getString("content"));
            	list.getList().add(gift);
			}
        	
        } catch (NullPointerException e) {
            list = new GiftList();
        }
		return list;
	}

	@Override
    protected GiftList readList(Serializable seri) {
        return ((GiftList) seri);
    }

	@Override
	protected void sendRequestData() {
		//SouDianAPI.getApkList(mCatalog, mCurrentPage, mHandler);
		SouDianAPI.getAppZone(mHandler);
	}

	@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Gift gift = mAdapter.getItem(position);
        if (gift != null) {
        	//UIHelper.showSimpleBack(getActivity() ,SimpleBackPage.ALL_APK , position  + 1);
        	UIHelper.showApkList(getActivity(), position);
        }
    }

	@Override
    protected void executeOnLoadDataSuccess(List<Gift> data) {
        if (mCatalog == GiftList.CATALOG_ALL) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            if (mState == STATE_REFRESH)
                mAdapter.clear();
            mAdapter.addData(data);
            mState = STATE_NOMORE;
            mAdapter.setState(ListBaseAdapter.STATE_NO_MORE);
            return;
        }
        super.executeOnLoadDataSuccess(data);
    }

	@Override
	protected long getAutoRefreshTime() {
		// 最新apk两小时刷新一次
		if (mCatalog == ApkList.CATALOG_ALL) {
			return 2 * 60 * 60;
		}
		return super.getAutoRefreshTime();
	}
}
